package com.wish.finance.controller;


import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wish.common.core.usconfig.common.MsgResult;
import com.wish.finance.entity.TCwPayOrderVO;
import com.wish.finance.service.ITCwPayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 支付订单表（支付流水信息） 前端控制器
 * </p>
 *
 * @author
 * @since 2022-03-27
 */
@RestController
@RequestMapping("/payorder")
public class TCwPayOrderController {

	@Autowired
	ITCwPayOrderService cwPayOrderService;

	@PostMapping("list")
	public MsgResult list(@RequestBody TCwPayOrderVO vo) {
		IPage<TCwPayOrderVO> page = cwPayOrderService.listAll(vo);
		return MsgResult.succeed(page);
	}

	@PostMapping("getExcel")
	public void getExcel(HttpServletResponse response) throws IOException {
		List<TCwPayOrderVO> list = cwPayOrderService.listExcel();
		// 通过工具类创建writer，默认创建xls格式
		ExcelWriter writer = ExcelUtil.getWriter();
		//自定义标题别名
		writer.addHeaderAlias("orderNo", "交易流水号");
		writer.addHeaderAlias("businessplan", "业务订单号");
		writer.addHeaderAlias("tradeAccountTypeName", "交易账户类型");
		writer.addHeaderAlias("userTypeName", "用户类型");
		writer.addHeaderAlias("tradeAccountNo", "交易账号");
		writer.addHeaderAlias("tradeAccount", "交易账户");
		writer.addHeaderAlias("payWayName", "支付方式");
		writer.addHeaderAlias("finalAmount", "结算金额");
		writer.addHeaderAlias("tradeUser", "交易对手");
		writer.addHeaderAlias("dsAccount", "对手账号");
		writer.addHeaderAlias("tradeQudao", "交易渠道");
		writer.addHeaderAlias("qudaoOrderNo", "渠道订单号");
		writer.addHeaderAlias("tradeResultName", "交易结果");
		writer.addHeaderAlias("returnCode", "返回码");
		writer.addHeaderAlias("returnMsg", "返回信息");
		writer.addHeaderAlias("datefq", "发起交易日期");
		writer.addHeaderAlias("datezx", "执行交易日期");
		writer.addHeaderAlias("datejz", "支付截止日期");
		writer.addHeaderAlias("tradeRate", "费率");
		writer.addHeaderAlias("commissionFee", "手续费");
		writer.addHeaderAlias("amount", "订单金额");
		writer.addHeaderAlias("payAmount", "支付金额");

		// 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
		writer.setOnlyAlias(true);

		// 一次性写出内容，使用默认样式，强制输出标题
		writer.write(list, true);
		//out为OutputStream，需要写出到的目标流

		//response为HttpServletResponse对象
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		//test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
		response.setHeader("Content-Disposition", "attachment;filename=test.xls");
		ServletOutputStream out = response.getOutputStream();

		writer.flush(out, true);
		// 关闭writer，释放内存
		writer.close();
		//此处记得关闭输出Servlet流
		IoUtil.close(out);
	}

}
